import { createRouter, createWebHistory } from 'vue-router'

// 登录页面
import login from './views/login.vue'

// 组织架构管理相关路由
import DepartmentManagement from './views/DepartmentManagement.vue'
import ApplicationManagement from './views/ApplicationManagement.vue'
import UserManagement from './views/UserManagement.vue'

// 任务角色管理相关路由
import RoleDefinition from './views/RoleDefinition.vue'
import RouteFieldConfiguration from './views/RouteFieldConfiguration.vue'
import RoleRouteManagement from './views/RoleRouteManagement.vue'
import RoutingRules from './views/RoutingRules.vue'

// 数据源配置相关路由
import DataSourceConfiguration from './views/DataSourceConfiguration.vue'
// 表单设计器
import FormDesigner from './views/FormDesigner.vue'
// 任务流程设计器
import TaskProcessDesigner from './views/TaskProcessDesigner.vue'
// 任务管理
import TaskManagement from './views/TaskManagement.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    // 登录页面
    {
      path: '/login',
      name: 'login',
      component: login,
      meta: { requiresNoAuth: true }
    },
    // 组织架构管理
    {
      path: '/department-management',
      name: 'DepartmentManagement',
      component: DepartmentManagement,
      meta: { requiresAuth: true }
    },
    {
      path: '/application-management',
      name: 'ApplicationManagement',
      component: ApplicationManagement,
      meta: { requiresAuth: true }
    },
    {
      path: '/user-management',
      name: 'UserManagement',
      component: UserManagement,
      meta: { requiresAuth: true }
    },
    // 任务角色管理
    {
      path: '/role-definition',
      name: 'RoleDefinition',
      component: RoleDefinition,
      meta: { requiresAuth: true }
    },
    {
      path: '/route-field-configuration',
      name: 'RouteFieldConfiguration',
      component: RouteFieldConfiguration,
      meta: { requiresAuth: true }
    },
    {
      path: '/role-route-management',
      name: 'RoleRouteManagement',
      component: RoleRouteManagement,
      meta: { requiresAuth: true }
    },
    {
      path: '/routing-rules',
      name: 'RoutingRules',
      component: RoutingRules,
      meta: { requiresAuth: true }
    },
    // 数据源配置
    {
      path: '/data-source-configuration',
      name: 'DataSourceConfiguration',
      component: DataSourceConfiguration,
      meta: { requiresAuth: true }
    },
    // 表单设计器
    {
      path: '/form-designer/:taskId?',
      name: 'FormDesigner',
      component: FormDesigner,
      meta: { requiresAuth: true }
    },
    // 任务流程设计器
    {
      path: '/task-process-designer',
      name: 'TaskProcessDesigner',
      component: TaskProcessDesigner,
      meta: { requiresAuth: true }
    },
    // 任务管理
    {
      path: '/task-management',
      name: 'TaskManagement',
      component: TaskManagement,
      meta: { requiresAuth: true }
    }
  ]
})

// 路由守卫 - 检查用户是否已登录
router.beforeEach((to, from, next) => {
  const isAuthenticated = !!localStorage.getItem('user');
  
  // 如果路由需要认证但用户未登录，则重定向到登录页面
  if (to.meta.requiresAuth && !isAuthenticated) {
    next('/login');
  }
  // 如果路由需要未认证（如登录页面）但用户已登录，则重定向到数据源配置页面
  else if (to.meta.requiresNoAuth && isAuthenticated) {
    next('/data-source-configuration');
  }
  else {
    next();
  }
});

export default router